package com.windoent.yugioh.ui.activity.setting;

import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.baidu.mobstat.StatService;
import com.google.android.material.imageview.ShapeableImageView;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.ContactUsBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.utils.some.utilcode.util.ClipboardUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.ContactUsItem;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/12 18:00
 * desc   :联系我们
 */
public class ContactUsActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private ContactUsItem mItemOffical;
    private ContactUsItem mItemEmail;
    private ContactUsItem mItemGongzhonghao;
    private ContactUsItem mItemQq;
    private ShapeableImageView mImage;


    @Override
    public int getLayout() {
        return R.layout.activity_setting_contact_us;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mItemOffical = (ContactUsItem) findViewById(R.id.item_offical);
        mItemEmail = (ContactUsItem) findViewById(R.id.item_email);
        mItemGongzhonghao = (ContactUsItem) findViewById(R.id.item_gongzhonghao);
        mItemQq = (ContactUsItem) findViewById(R.id.item_qq);
        mImage = (ShapeableImageView) findViewById(R.id.image);
    }

    //联系我们
    private void initUi(ContactUsBean.DataBean.ContactBean contact) {
        setText(mItemOffical.getTextView(), BaseMethod.notNullDefaultValue(contact.getWebSite(), getString(R.string.string_interval)));
        setText(mItemEmail.getTextView(), BaseMethod.notNullDefaultValue(contact.getEmail(), getString(R.string.string_interval)));
        setText(mItemGongzhonghao.getTextView(), BaseMethod.notNullDefaultValue(contact.getPublicAccount(), getString(R.string.string_interval)));
        setText(mItemQq.getTextView(), BaseMethod.notNullDefaultValue(contact.getQq(), getString(R.string.string_interval)));
        SimpleUtils.loadImageForView(mContext, mImage, contact.getLogo(), R.drawable.img_official_avatar_android);
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mItemOffical.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse(mItemOffical.getTvContent());
                Intent it = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(it);
            }
        });
        mItemEmail.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardUtils.copyText(mItemEmail.getTvContent());
                ToastUtils.showText(getString(R.string.string_copy_to_clipboard));
            }
        });
        mItemGongzhonghao.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardUtils.copyText(mItemGongzhonghao.getTvContent());
                ToastUtils.showText(getString(R.string.string_copy_to_clipboard));
            }
        });
        mItemQq.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardUtils.copyText(mItemQq.getTvContent());
                ToastUtils.showText(getString(R.string.string_copy_to_clipboard));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "设置联系我们界面");
        getContactUsData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "设置联系我们界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    private void getContactUsData() {
        showLoadingDialog("");
        PhoneLive.getContactUsData(getUserToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            ContactUsBean json = gson.fromJson(response, ContactUsBean.class);
                            if (json != null) {
                                ContactUsBean.DataBean data = json.getData();
                                if (data != null) {
                                    ContactUsBean.DataBean.ContactBean contact = data.getContact();
                                    if (contact != null) {
                                        initUi(contact);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }


}
